package com.ssm.handle;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class MyMetaObjectHandle implements MetaObjectHandler {
    /*包名要建在com.ssm下，因为user模块导入了mp模块且在user模块启动类上配置了ComponentScan包扫描com.ssm下的所有文件，
    如果该类不在com.ssm下会扫描不到,且userPo实体类的属性上加入@TableField注解 并声明什么操作生效*/

    //MetaObjectHandler接口是mybatisPlus为我们提供的的一个扩展接口，我们可以利用这个接口在我们插入或者更新数据的时候，为一些字段指定默认值

    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createBy", String.class, "ssm");
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "deleteFlag", Integer.class, 0);
        this.strictInsertFill(metaObject, "version", Integer.class, 0);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updateBy", String.class, "ssm");
        this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
    }
}
